<?php
namespace app\index\model;
class Jc extends BaseModel{
    function getJCAll(){
        $jc=D("qi");
        return $jc->where("status",1)->select();
    }
    
    function getJcAllHis($page=1){
		$jc=D("qi");
        
        $res = $jc->where("status",1)->page($page,10)->order('create_time desc')->select();
        foreach ($res as &$tmp) {
            $str = $tmp['result'];
            $first = intval(str_get_last($str, 3));
            $second = intval(str_get_last($str, 2));
            $last = intval(str_get_last($str, 1));
            
            $totla = $first+$second+$last;
            if (($last==$second)){
                $name = "合";
            }else {
                if ($totla<14){
                    if ($totla%2==0){
                        $name = "小双";
                    }else{
                        $name = "小单";
                    }
            
                }else {
                    if ($totla%2==0){
                        $name = "大双";
                    }else{
                        $name = "大单";
                    }
                }
            }
            $tmp['he_res'] = $name;
            $tmp['he'] = $totla;
        }
        unset($tmp);
        return $res;
    }
    function getMyJCAll($id,$page=1){
        $jc=D("qi");
        $res = $jc->alias("a")
        ->join("__QIDEATILS__ b","a.id=b.qi_id")
        ->where("b.user_id",$id)
        ->where("a.status",1)
        ->order("a.create_time desc")
        ->page($page,10)
        ->select();
        foreach ($res as &$tmp) {
            $tmp['jc_name'] = $this->getjcname($tmp);
            $tmp['res_name'] = $this->getresname($tmp);
            $a = $this->checkres($tmp);
            if($a){
                $tmp['is_ying']=1;
                $tmp['ying_money']=$tmp['money']*$tmp['peilv']/10000;
            }else{

                $tmp['is_ying']=0;
                $tmp['ying_money']=$tmp['money']/100;
            }
            $str = $tmp['result'];
            $tmp['result'] = str_get_last($str, 3).str_get_last($str, 2).str_get_last($str, 1);
        }
        unset($tmp);
        return $res;
    }
    
    
    function getresname($tmp){
        $str = $tmp['result'];
        $first = intval(str_get_last($str, 3));
        $second = intval(str_get_last($str, 2));
        $last = intval(str_get_last($str, 1));
        $totla = $first+$second+$last;
        $name = "";
        switch ($tmp['type']){
            case 1:
                if (($last==$second)){
                    $name = "合";
                }else {
                    if ($last<5){
                        $name = "小";
                    }else {
                        $name = "大";
                    }
                }
                break;
            case 2:

                if (($last==$second)){
                    $name = "合";
                }else {
                    if ($totla<14){
                        if ($totla%2==0){
                            $name = "小双";
                        }else{
                            $name = "小单";
                        }
                        
                    }else {
                    if ($totla%2==0){
                            $name = "大双";
                        }else{
                            $name = "大单";
                        }
                    }
                }
                break;
            case 3:

                if (($last==$second)){
                    $name = "合";
                }else {
                    $name="".$last;
                }
                break;
            
        }
        return $name;
    }

    function getjcname($tmp){
        
        $name = "";
        switch ($tmp['xiazhu_type']){
            case 1:
               $name="小";
                break;
            case 2:
               $name="大";
                break;
            case 3:
               $name="小双";
                break;
            case 4:
               $name="小单";
                break;
            case 5:
               $name="大单";
                break;
            case 6:
               $name="大双";
                break;
            case 8:
               $name=$tmp['num'];
                break;
            case 9:
               $name="合";
                break;
        }
        return $name;
    }
    
    //1：小，2：大，3：小双，4：小单，5：大单，6：大双，8：精准，9：合
    function checkres($tmp){
        $str = $tmp['result'];
        $first = intval(str_get_last($str, 3));
        $second = intval(str_get_last($str, 2));
        $last = intval(str_get_last($str, 1));
        $totla = $first+$second+$last;
        $res = false;
       switch ($tmp['xiazhu_type']){
           case 1:
               if (($last!=$second)&&$last<5){
                   $res = true;
               }
               break;
           case 2:

               if (($last!=$second)&&$last>4){
                   $res = true;
               }
               break;
           case 3:

               if (($last!=$second)&&$totla<14&&($totla%2==0)){
                   $res = true;
               }
               break;
           case 4:

       

               if (($last!=$second)&&$totla<14&&($totla%2!=0)){
                   $res = true;
               }
               break;
           case 5:


               if (($last!=$second)&&$totla>13&&($totla%2!=0)){
                   $res = true;
               }
               break;
           case 6:
               if (($last!=$second)&&$totla>13&&($totla%2==0)){
                   $res = true;
               }
              
               break;
           case 8:

        if (($last!=$second)&&$last==intval($tmp['num'])){
                   $res = true;
               }
               break;
           case 9:
               if ($last==$second){
                   $res = true;
               }
               break;
       }
       return $res;
        
    }
    
    function finish($id){
        $jc=D("qi");
        $res = $jc->where("id",$id)->find();
        $res['status'] = 1;
        $jc->update($res);
    }
    function getJC($id){
        $jc=D("qi");
        return $jc->where("id",$id)->find();
    }
    function checkixist($id,$user_id,$type,$isjz=null){
        $jc=D("qideatils");
        if ($isjz===null){
            return $jc->where("xiazhu_type",$type)->where("user_id",$user_id)->where("qi_id",$id)->find();
        }else {

            return $jc->where("xiazhu_type",$type)->where("user_id",$user_id)->where("num",$isjz)->where("qi_id",$id)->find();
        }
        
    }
    
    function addJCDetails($data){
        $res = null;
        if ($data['xiazhu_type']==8){
            $res = $this->checkixist($data['qi_id'], $data['user_id'], $data['xiazhu_type'],$data['num']);
        }else {
            $res =$this->checkixist($data['qi_id'], $data['user_id'], $data['xiazhu_type']);
        }
        if (!$res){
			
            $jc=D("qideatils");
            $data['id'] = uuidBy32();
            $jc->insert($data);
        }else {
            $jc=D("qideatils");
            $jc->where("id",$res['id'])->setInc('money', $data['money']);
        }
        
    }
    
    
    function addPerson($id,$user_id){
        $qideatils=D("qideatils");
        $res = $qideatils->where("qi_id",$id)->where("user_id",$user_id)->find();
        if(!$res){
            $jc=D("qi");
            $jc->where("id",$id)->setInc("person");
        }
    }
    function addMoney($id,$money){
        $jc=D("qi");
        $jc->where("id",$id)->setInc("total_money",$money);
    }
    function getJCDetailsAll($id,$where,$num=null){
        $jc=D("qideatils");
        if ($num===null){
            return $jc->where($where)->select();
        }else {
            return $jc->where($where)->select();
        }
    }
    
    function getsomebodyDeatil($id,$user_id){
        $jc=D("qi");
        $res = $jc->alias("a")
        ->join("__QIDEATILS__ b","a.id=b.qi_id")
        ->where("b.user_id",$user_id)
        ->where("a.id",$id)
        ->order("a.create_time desc")
        ->select();
        $counts  = 0;
        foreach ($res as &$tmp) {
            $tmp['jc_name'] = $this->getjcname($tmp);
            $tmp['res_name'] = $this->getresname($tmp);
            $a = $this->checkres($tmp);
            if($a){
                $counts++;
                $tmp['is_ying']=1;
                $tmp['ying_money']=$tmp['money']*$tmp['peilv']/10000;
            }else{
        
                $tmp['is_ying']=0;
                $tmp['ying_money']=$tmp['money']/100;
            }
            $str = $tmp['result'];
            $tmp['result'] = str_get_last($str, 3).str_get_last($str, 2).str_get_last($str, 1);
        }
        unset($tmp);
        
        
        $d['count'] = $counts;
        $d['data'] = $res;
        return $d;
    }
    
    
    
    function addJC($times,$before){
        $nowdate = getnowtime();
        $qi_name = "";
        if (!$before||$before==""){
            $qi_name = $nowdate."0001";
        }else {
            $beforedate = substr($before,0,8);
            $beforenum = substr($before,8,4);
            if ($beforedate!=$nowdate){
                $qi_name = $nowdate."0001";
            }else {
               $nownumtmp = "".(intval($beforenum)+1);
               $nownum=$nownumtmp;
               for($i=0;$i<(4-strlen($nownumtmp));$i++){
                   $nownum = "0".$nownum;
               }
               $qi_name = $nowdate.$nownum;
            }
        }
        
        
        
        
        
        
        $jc=D("qi");
        $data['name'] = $qi_name;
        $now = time();
        $data['create_time'] = date('Y-m-d H:i:s',$now);
        $data['end_time'] = date('Y-m-d H:i:s',$now+$times);
        $data['id']=$jc->insertGetId($data);
        return ["id"=>$data['id'],"time"=>strtotime($data['create_time']),"name"=>$data['name']];
    }
    /**
     * 是否已经开奖
     * @param unknown $id
     */
    function checkStatus($id){
       $jcinfo=D("qi");
       $qiinfo = $jcinfo->where("id",$id)->find();
       if($qiinfo&&$qiinfo['status']==0){
           return false;
       } 
       return true;
    }
    function checkALl($id){
        $jcinfo=D("qi");
        $qiinfo = $jcinfo->where("id",$id)->find();
        if (!$qiinfo){
            return false;
        }
        if ($qiinfo['result']||$qiinfo['is_hand']==1){
            return $qiinfo;
        }
        $jc=D("qideatils");
        $total = $jc->where("qi_id",$id)->sum("money");
        if ($total==null||$total==0){
            $jcinfo=D("qi");
            $qiinfo['result'] = $this->rand6().rand(0, 9).rand(0, 9).rand(0, 9);
            $jcinfo->update($qiinfo);
            return $qiinfo;
        }
        //1：小，2：大，3：小双，4：小单，5：大单，6：大双，8：精准，9：合
        $data["small"] = $jc->where("xiazhu_type",1)->where("qi_id",$id)->sum("money * peilv /100");
        $data["big"] = $jc->where("xiazhu_type",2)->where("qi_id",$id)->sum("money * peilv /100");
        $data["smallDouble"] = $jc->where("xiazhu_type",3)->where("qi_id",$id)->sum("money * peilv /100");
        $data["smallSingle"] = $jc->where("xiazhu_type",4)->where("qi_id",$id)->sum("money * peilv /100");
        $data["bigSingle"] = $jc->where("xiazhu_type",5)->where("qi_id",$id)->sum("money * peilv /100");
        $data["bigDouble"] = $jc->where("xiazhu_type",6)->where("qi_id",$id)->sum("money * peilv /100");
        //精准5-9
        $jz2 = $this->getmingt4($jc,$id);
        //精准0-4
        $jz1 = $this->getminlt5($jc,$id);
        $data["he"] = $jc->where("xiazhu_type",9)->where("qi_id",$id)->sum("money * peilv /100");
        foreach ($data as &$tmp){
            if ($tmp==null){
                $tmp=0;
            }
        }
        unset($tmp);
        $kaijiang = $this->countresult($total,$data,$jz1,$jz2);
        $jcinfo=D("qi");
        $qiinfo['result'] = $kaijiang['res'];
        $qiinfo['total_money'] = $total;
        $qiinfo['yingli_money'] = $kaijiang['yingli'];
        $jcinfo->update($qiinfo);
        return $qiinfo;
    }
    
    function countresult($total,$data,$jz1,$jz2){
       return $this->array_hebing($total, $data, $jz1, $jz2);
    }
    
    
    function array_hebing($total,$data,$jz1,$jz2){
        $small1 = $this->small1($total, $data, $jz1);
        $small2 = $this->small2($total, $data, $jz1);
        $small3 = $this->small3($total, $data, $jz1);
        $small4 = $this->small4($total, $data, $jz1);
        $big1 = $this->big1($total, $data, $jz2);
        $big2 = $this->big2($total, $data, $jz2);
        $big3 = $this->big3($total, $data, $jz2);
        $big4 = $this->big4($total, $data, $jz2);
        $arr = array_merge($small1,$small2,$small3,$small4,$big1,$big2,$big3,$big4);
        if ($data['he']<=$total){
            $he['yingli'] = round($total-$data['he'],2);
            $he['res'] = $this->randhe();
            array_push($arr, $he);
        }
        $randI = rand(0, count($arr));
        $arr[$randI]['res'] = $this->rand6().$arr[$randI]['res'];
        return $arr[$randI];
    }
    
    /*
     *小(大)，小单，精准 
     */
    function small1($total,$data,$jz1){
        $iscan = [];
        foreach ($jz1 as $tmp){
            $tmptotal = $tmp['money']+$data['small']+$data['smallSingle'];
            if ($total>=$tmptotal){
                $last = $tmp['num'];
                $middle = $this->rand_not_he($last,true,false);
                $first = $this->getsmallfirst(intval($last),intval($middle),false);
                $result['yingli'] = round($total-$tmptotal,2);
                $result['res'] = $first.$middle.$last;
                array_push($iscan, $result);
            }
        }
        return $iscan;
    }
    /*
     *小(大)，小双，精准
     */
    function small2($total,$data,$jz1){
        $iscan = [];
        foreach ($jz1 as $tmp){
            $tmptotal = $tmp['money']+$data['small']+$data['smallDouble'];
            if ($total>=$tmptotal){
                $last = $tmp['num'];
                $middle = $this->rand_not_he($last,true,true);
                $first = $this->getsmallfirst(intval($last),intval($middle),true);
                $result['yingli'] = round($total-$tmptotal,2);
                $result['res'] = $first.$middle.$last;
                array_push($iscan, $result);
            }
        }
        return $iscan;
    }
    /*
     *小(大)，大单，精准 
     */
    function small3($total,$data,$jz1){
        $iscan = [];
        foreach ($jz1 as $tmp){
            $tmptotal = $tmp['money']+$data['small']+$data['bigSingle'];
            if ($total>=$tmptotal){
                $last = $tmp['num'];
                $middle = $this->rand_not_he($last,false,false);
                $first = $this->getbigfirst(intval($last),intval($middle),false);
                $result['yingli'] = round($total-$tmptotal,2);
                $result['res'] = $first.$middle.$last;
                array_push($iscan, $result);
            }
        }
        return $iscan;
    }
    /*
     *小(大)，大双，精准
     */
    function small4($total,$data,$jz1){
        $iscan = [];
        foreach ($jz1 as $tmp){
            $tmptotal = $tmp['money']+$data['small']+$data['bigDouble'];
            if ($total>=$tmptotal){
                $last = $tmp['num'];
                $middle = $this->rand_not_he($last,false,true);
                $first = $this->getbigfirst(intval($last),intval($middle),true);
                $result['yingli'] = round($total-$tmptotal,2);
                $result['res'] = $first.$middle.$last;
                array_push($iscan, $result);
            }
        }
        return $iscan;
    }

    /*
     *小(大)，小单，精准
     */
    function big1($total,$data,$jz1){
        $iscan = [];
        foreach ($jz1 as $tmp){
            $tmptotal = $tmp['money']+$data['big']+$data['smallSingle'];
            if ($total>=$tmptotal){
                $last = $tmp['num'];
                $middle = $this->rand_not_he($last,true,false);
                $first = $this->getsmallfirst(intval($last),intval($middle),false);
                $result['yingli'] = round($total-$tmptotal,2);
                $result['res'] = $first.$middle.$last;
                array_push($iscan, $result);
            }
        }
        return $iscan;
    }
    /*
     *小(大)，小双，精准
     */
    function big2($total,$data,$jz1){
        $iscan = [];
        foreach ($jz1 as $tmp){
            $tmptotal = $tmp['money']+$data['big']+$data['smallDouble'];
            if ($total>=$tmptotal){
                $last = $tmp['num'];
                $middle = $this->rand_not_he($last,true,true);
                $first = $this->getsmallfirst(intval($last),intval($middle),true);
                $result['yingli'] = round($total-$tmptotal,2);
                $result['res'] = $first.$middle.$last;
                array_push($iscan, $result);
            }
        }
        return $iscan;
    }
    /*
     *小(大)，大单，精准
     */
    function big3($total,$data,$jz1){
        $iscan = [];
        foreach ($jz1 as $tmp){
            $tmptotal = $tmp['money']+$data['big']+$data['bigSingle'];
            if ($total>=$tmptotal){
                $last = $tmp['num'];
                $middle = $this->rand_not_he($last,false,false);
                $first = $this->getbigfirst(intval($last),intval($middle),false);
                $result['yingli'] = round($total-$tmptotal,2);
                $result['res'] = $first.$middle.$last;
                array_push($iscan, $result);
            }
        }
        return $iscan;
    }
    /*
     *小(大)，大双，精准
     */
    function big4($total,$data,$jz1){
        $iscan = [];
        foreach ($jz1 as $tmp){
            $tmptotal = $tmp['money']+$data['big']+$data['bigDouble'];
            if ($total>=$tmptotal){
                $last = $tmp['num'];
                $middle = $this->rand_not_he($last,false,false);
                $first = $this->getbigfirst(intval($last),intval($middle),true);
                $result['yingli'] = round($total-$tmptotal,2);
                $result['res'] = $first.$middle.$last;
                array_push($iscan, $result);
            }
        }
        return $iscan;
    }
    function getsmallfirst($last,$middle,$isDouble=false){
        $arrTmp = [0,1,2,3,4,5,6,7,8,9];
        $arr = [];
        $arr_ou = [];
        foreach ($arrTmp as $tmp){
            $total = $tmp+$last+$middle;
            if ($total<14){
                if($total%2==0){
                    array_push($arr_ou, $tmp);
                }else {
                    array_push($arr, $tmp);
                }
            }
        }
        $randI = rand(0, count($arr)-1);
        $randI_ou = rand(0, count($arr_ou)-1);
        if ($isDouble){
            return $arr_ou[$randI_ou];
        }else {

            
            return $arr[$randI];
        }
        
    }
    
    function getbigfirst($last,$middle,$isDouble=false){
        $arrTmp = [0,1,2,3,4,5,6,7,8,9];
        $arr = [];
        $arr_ou = [];
        foreach ($arrTmp as $tmp){
            $total = $tmp+$last+$middle;
            if ($total>13){
                if($total%2==0){
                    array_push($arr_ou, $tmp);
                }else {
                    array_push($arr, $tmp);
                }
            }
        }
        $randI = rand(0, count($arr)-1);
        $randI_ou = rand(0, count($arr_ou)-1);
        if ($isDouble){
            return $arr_ou[$randI_ou];
        }else {
        
            return $arr[$randI];
        }
    }
    
    /**
     * 
     * @param unknown $last最后一位数
     * @param string $max是否是小单，小双
     * @param string $smallDouble，是否是双
     * @return Ambigous <>
     */
    function rand_not_he($last,$max=false,$smallDouble=false){
        $arrTmp = [0,1,2,3,4,5,6,7,8,9];
        $arr = [];
        foreach ($arrTmp as $tmp){
            if ($last!=$tmp){
                if($max){
                    if($smallDouble){
                        if(($last+$tmp)<13){
                            array_push($arr, $tmp);
                        }
                        
                    }else{
                        if(($last+$tmp)<=13){
                            array_push($arr, $tmp);
                        }
                    }
                }else{
                    if($smallDouble){
                        if(($last+$tmp)>4){
                            array_push($arr, $tmp);
                        }
                    }else{
                        if(($last+$tmp)>5){
                            array_push($arr, $tmp);
                        }
                    }
                    
                }
                
            }            
        }
        $randI = rand(0, count($arr)-1);
        return $arr[$randI];
    }
   
    /**0-4
     * 获取精确，个位，
     * 以及哪些没有押
     * 
     */
    function getminlt5($jc,$id){
        $data = [];
        for($i=0;$i<5;$i++){
            $res= $jc->where("num",$i)->where("qi_id",$id)->where("xiazhu_type",8)->sum("money * peilv /100");
            if ($res==null||$res==0){
               $tmp['money']=0;
            }else {
                $tmp['money']=$res;
            }
            $tmp['num']=$i;
            array_push($data, $tmp);
        }
        
        return $data;
    }
    /**5-9
     * 获取精确，个位，
     * 以及哪些没有押
     *
     */
    function getmingt4($jc,$id){
        $data = [];
        for($i=5;$i<10;$i++){
            $res= $jc->where("num",$i)->where("qi_id",$id)->where("xiazhu_type",8)->sum("money * peilv /100");
            if ($res==null||$res==0){
               $tmp['money']=0;
            }else {
                $tmp['money']=$res;
            }
            $tmp['num']=$i;
            array_push($data, $tmp);
        }
        
        return $data;
    }

    function rand6(){
        $res = "";
        for ($i=0;$i<6;$i++){
            if($i==0){
                $res=$res.rand(1, 9);
            }else{
                $res=$res.rand(0, 9);
            }
            
        }
        return $res;
    }
    
    function randhe(){
        $first = rand(0, 9);
        $secong =  rand(0, 9);
        return $first.$secong.$secong;
    }
}
